{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Interfaces" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Interfaces are used to define a set of methods that a class must have in order to be a valid type of that class.\n", "\n", "1. define the `interface`\n", "2. define a class that `extends` that interface\n", "\n", "First we define an interface:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added interface Human\n", "\n" ] } ], "source": [ "interface Human {\n", " public abstract double age();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What can we do with it?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Human is sorta like a type/class... we spell it with a capital letter...." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "| Error:\n", "| Human is abstract; cannot be instantiated\n", "| new Human()\n", "| ^---------^\n", "\n" ] } ], "source": [ "new Human()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "No, you can create an instance of an interface. But you can create a class that implements it:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "| Error:\n", "| Student is not abstract and does not override abstract method age() in Human\n", "| class Student implements Human {\n", "| ^-------------------------------...\n", "\n" ] } ], "source": [ "class Student implements Human {\n", "\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...but only if you satisfy the requirements of the interface:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added class Student\n", "\n" ] } ], "source": [ "class Student implements Human {\n", " public double age() {\n", " return 19.0;\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, you can use the class as you would normally:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable student of type Student with initial value Student@4ca8195f\n", "\n" ] } ], "source": [ "Student student = new Student();" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: 19.0\n", "| assigned to temporary variable $4 of type double\n", "\n" ] }, { "data": { "text/plain": [ "19.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "student.age();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition, you can \"cast\" a class as an interface. If you do that, you can only use methods that exist in Human:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable human of type Human with initial value Student@4ca8195f\n", "\n" ] } ], "source": [ "Human human = student;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also cast it explicitly like: `((Human)student)`" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: 19.0\n", "| assigned to temporary variable $6 of type double\n", "\n" ] }, { "data": { "text/plain": [ "19.0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "human.age()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Replaced class Student\n", "| Update replaced variable student, reset to null\n", "| Update overwrote class Student\n", "\n" ] } ], "source": [ "class Student implements Human {\n", " public double age() {\n", " return 19.0;\n", " }\n", " public boolean in_cs206() {\n", " return true;\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable student of type Student with initial value Student@d70c109\n", "\n" ] } ], "source": [ "Student student = new Student();" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: true\n", "| assigned to temporary variable $9 of type boolean\n", "\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "student.in_cs206();" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added class Teacher\n", "\n" ] } ], "source": [ "class Teacher implements Human {\n", " public double age() {\n", " return 52.0;\n", " }\n", " public boolean has_tenure() {\n", " return true;\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable teacher of type Teacher with initial value Teacher@2d6a9952\n", "\n" ] } ], "source": [ "Teacher teacher = new Teacher();" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: 52.0\n", "| assigned to temporary variable $12 of type double\n", "\n" ] }, { "data": { "text/plain": [ "52.0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "teacher.age()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Added variable alist of type ArrayList with initial value []\n", "\n" ] } ], "source": [ "ArrayList alist = new ArrayList();" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: true\n", "| assigned to temporary variable $14 of type boolean\n", "\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alist.add(teacher);" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: true\n", "| assigned to temporary variable $15 of type boolean\n", "\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alist.add(student);" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Age: 52.0\n", "Age: 19.0\n", "\n" ] } ], "source": [ "for (Human human: alist) {\n", " printf(\"Age: %s\\n\", human.age());\n", "}" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Modified variable human of type Human with initial value Teacher@2d6a9952\n", "\n" ] } ], "source": [ "Human human = teacher;" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Expression value is: true\n", "| assigned to temporary variable $18 of type boolean\n", "\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "teacher.has_tenure()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*See page 6 of your textbook for more information.*" ] } ], "metadata": { "kernelspec": { "display_name": "Java 9", "language": "java", "name": "java9" }, "language_info": { "file_extension": ".class", "mimetype": "application/java-vm", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }